CASE statement in ST

Syntax
CASE ... OF
  ...
  ELSE ...
END_CASE
Meaning

Use the CASE statement to specify that a group of statements is to be executed, if the evaluated value of the selector (entered after CASE) corresponds to a label which is entered for this group of statements. If the value of the selector does not correspond to any of the labels, then either no statement is to be executed, or the statement group following the ELSE keyword is to be executed,

Restriction

You are only allowed to enter such →expressions for the selector that can be evaluated to be of the →generic data type ANY_INT.

A label may be one or more integer →literals/→variables, enumerated values or subranges and must be evaluated as →constant value during the runtime. The data type of the labels must be implicitly convertible into the data type of the selector.

Detailed explanation about CASE

Part of CASE statement

Explanation

CASE selector OF
  1: statements-A;
  2, 3: statements-B;
  4..6, 7: statements-C;
  ... 

 

  • If the value of the selector corresponds to label 1 (hence: in case of value = 1), the execution continues with the statement block statements-A. After processing the last statement, the execution continues after END_CASE.

  • In case of value = 2 or 3, the execution continues with the statement block statements-B. After processing the last statement, the execution continues after END_CASE.

  • In case of value = 4, 5, 6 or 7, the execution continues with the statement block statements-C. After processing the last statement, the execution continues after END_CASE.

Each statement block may contain as many statements as you like. You may insert as many labels and statement blocks as you like. If you enter the same value for labels, the statement block first entered is selected for execution because Neuron Power Engineer compares from top to bottom.

ELSE
statements-n;

 

The statement block behind ELSE is only processed, if none of the labels matches the value of the selector. The statement block may contain as many statements as you like. After processing the last statement, the execution continues after END_CASE.

You may skip the ELSE-part.

END_CASE;

 

This concludes the CASE-statement.

 
Example
FUNCTION_BLOCK ExampleIfAndCaseDocumentation
  VAR
      up : BOOL;
      count : INT;
      notify : STRING[1000];
  END_VAR
  VAR CONSTANT
      neg100 : int := -100;
      neg1 : int := -1;
  END_VAR
   
  IF up THEN          /* If 'up' = 'TRUE', counter counts up. */
      count := count + 1;
  ELSE
      count := count - 1;
  END_IF;
       
  CASE count OF      /* If 'count' is in the respective range of values, 'notify' displays the appropriate text. */
      0            : notify := 'Counter is 0.';
      1..100       : notify := 'Counter is in the range between 1 and 100.';    /* This label defines a subrange with integer values. */
      neg100..neg1 : notify := 'Counter is in the range between -100 and -1.';  /* This label defines a subrange with constant variables. */
  ELSE
      notify := 'Counter is more than 100 or less than -100.';
  END_CASE;
END_FUNCTION_BLOCK